package com.hubt.service.impl;

import java.awt.Menu;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hubt.common.PageBean;
import com.hubt.mapper.PtOrganMapper;
import com.hubt.model.OrganTree;
import com.hubt.model.PtOrgan;
import com.hubt.service.PtOrganService;
import com.hubt.vo.Organvo;

@Service("ptOrganService")
@Transactional
public class PtOrganServiceImpl implements PtOrganService {

	@Autowired
	private PtOrganMapper ptOrganMapper;

	@Override
	public PageBean<Organvo> findbyPage(int pageNumber, int pageSize) {
		List<PtOrgan> list = ptOrganMapper.findAllByPage((pageNumber-1)*pageSize, pageSize);
		List<Organvo> list2 = new ArrayList<Organvo>();
		for (PtOrgan organ : list) {
			Organvo organvo = new Organvo();
			organvo.setOrgan_uuid(organ.getOrganUuid());
			organvo.setOrgan_code(organ.getOrganCode());
			organvo.setOrgan_name(organ.getOrganName());
			organvo.setModtime(organ.getModtime());
			organvo.setOrgan_type(organ.getOrganType());
			organvo.setShortname(organ.getShortname());
			organvo.setIn_use(organ.getInUse());
			organvo.setParent_uuid(organ.getParentUuid());
			System.out.println(ptOrganMapper.selectByPrimaryKey(organ.getParentUuid()));

			if (ptOrganMapper.selectByPrimaryKey(organ.getParentUuid()) == null) {
				organvo.setParent_orgname(null);
			} else
				organvo.setParent_orgname(ptOrganMapper.selectByPrimaryKey(organ.getParentUuid()).getOrganName());

			list2.add(organvo);
		}
		PageBean<Organvo> pageBean = new PageBean<Organvo>();
		pageBean.setRows(list2);
		int total = ptOrganMapper.countSize();
		pageBean.setTotal(total);
		pageBean.setPageNumber(pageNumber);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage((total % pageNumber == 0) ? (total / pageNumber) : (total / pageSize + 1));
		return pageBean;

	}

	
	@Override
	public OrganTree findByparent(String id) {
		return getchild(id);
	}
	
	public OrganTree getchild(String id) {
		// TODO Auto-generated method stub
				OrganTree organTree=new OrganTree();
				if(id.equals("-1")) {
					organTree.setId("-1");
					organTree.setPid("-1");
					organTree.setName("国电集团");
				}else {
					PtOrgan ptOrgan=ptOrganMapper.selectByPrimaryKey(id);
					organTree.setId(ptOrgan.getOrganUuid());
					organTree.setPid(ptOrgan.getParentUuid());
					organTree.setName(ptOrgan.getOrganName());
				}
				List<PtOrgan> childList=ptOrganMapper.findByParentId(id);
				List<OrganTree> treelList=new ArrayList<OrganTree>();
				if(childList.size()==0)
					organTree.setHasChildren(false);
				else {
					organTree.setHasChildren(true);
				}
				for(int i=0;i<childList.size();i++) {
					OrganTree tmp=findByparent(childList.get(i).getOrganUuid());
					treelList.add(tmp);
				}
				organTree.setChildren(treelList);
				return organTree;
				}


	public boolean insert(Organvo organvo) {
		// TODO Auto-generated method stub
		PtOrgan ptOrgan=new PtOrgan();
		ptOrgan.setDescription(organvo.getDescription());
		ptOrgan.setAccountCode(organvo.getAccount_code());
		ptOrgan.setInUse(organvo.getIn_use());
		ptOrgan.setOrganCode(organvo.getOrgan_code());
		ptOrgan.setOrganName(organvo.getOrgan_name());
		ptOrgan.setOrganType(organvo.getOrgan_type());
		ptOrgan.setOrganUuid(organvo.getOrgan_uuid());
		ptOrgan.setParentUuid(organvo.getParent_uuid());
		ptOrgan.setShortname(organvo.getShortname());
		ptOrgan.setModtime(new Date());
		ptOrganMapper.insert(ptOrgan);
		return true;
	}


	@Override
	public Organvo findbyId(String uuid) {
		// TODO Auto-generated method stub
		PtOrgan ptOrgan=ptOrganMapper.selectByPrimaryKey(uuid);
		Organvo organvo=new Organvo();
		organvo.setOrgan_uuid(uuid);
		organvo.setOrgan_code(ptOrgan.getOrganCode());
		organvo.setOrgan_name(ptOrgan.getOrganName());
		organvo.setOrgan_type(ptOrgan.getOrganType());
		organvo.setIn_use(ptOrgan.getInUse());
		organvo.setParent_uuid(ptOrgan.getParentUuid());
		if(ptOrgan.getParentUuid().equals("-1"))
			organvo.setParent_orgname("root");
		else if(ptOrganMapper.selectByPrimaryKey(ptOrgan.getParentUuid())==null&&!ptOrgan.getParentUuid().equals("-1"))
				organvo.setParent_orgname(null);
			else {
				organvo.setParent_orgname(ptOrganMapper.selectByPrimaryKey(ptOrgan.getParentUuid()).getOrganName());
			}
				
		organvo.setAccount_code(ptOrgan.getAccountCode());
		organvo.setDescription(ptOrgan.getDescription());
		organvo.setShortname(ptOrgan.getShortname());
		System.out.println(ptOrgan.getShortname());
		return organvo;
	}


	@Override
	public boolean update(Organvo organvo) {
		// TODO Auto-generated method stub
		
		PtOrgan ptOrgan=new PtOrgan();
		ptOrgan.setDescription(organvo.getDescription());
		ptOrgan.setAccountCode(organvo.getAccount_code());
		ptOrgan.setInUse(organvo.getIn_use());
		ptOrgan.setOrganCode(organvo.getOrgan_code());
		ptOrgan.setOrganName(organvo.getOrgan_name());
		ptOrgan.setOrganType(organvo.getOrgan_type());
		ptOrgan.setOrganUuid(organvo.getOrgan_uuid());
		ptOrgan.setParentUuid(organvo.getParent_uuid());
		ptOrgan.setShortname(organvo.getShortname());
		try {
			ptOrganMapper.updateByPrimaryKeySelective(ptOrgan);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
//		ptOrganMapper.updateByPrimaryKeySelective(ptOrgan);
		
	}


	@Override
	public boolean delete(String id) {
		// TODO Auto-generated method stub
		try {
			ptOrganMapper.deleteByPrimaryKey(id);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	

}
